home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / x25 / nrs.shar.Z / nrs.shar / decompress.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-27  |  2.2 KB  |  81 lines

  1. #ifndef lint
  2. static RCSid[]="@(#)$Header: decompress.c,v 3.1 86/10/15 08:14:48 pb Rel $"
  3. #endif    lint
  4.  
  5. /***********************************************************************\
  6. *                                     *
  7. * A simple program to expand a Prime file in compressed format        *
  8. *                                     *
  9. * Copyright 1986, Piete Brooks, Julian Onions & Adrian Pell        *
  10. * pb@cl.cam.ac.uk, jpo@hcig.nott.ac.uk, Adrian.R.Pell@reading.ac.uk    *
  11. *                                    *
  12. * This program may be copied as long as you don't remove this notice,    *
  13. * try to make any money off of it, or pretend that you wrote it.    *
  14. *                                     *
  15. \***********************************************************************/
  16.  
  17. #include <stdio.h>
  18.  
  19. #define    OK                          0
  20. #define    WRONG_NUMBER_OF_ARGUMENTS   1
  21. #define    BAD_INPUT_FILE              2
  22. #define    BAD_OUTPUT_FILE             3
  23.  
  24. #define    USAGE       "Usage: %s inputfile outputfile\n"
  25.  
  26. FILE *fopen ();
  27.  
  28. main (argc, argv)
  29. int  argc;
  30. char **argv;
  31. {  FILE *input, *output;
  32.  
  33. /* Check if he has the correct number of arguments or if
  34.  * he has specied a leading '-'. If either give a usage message.
  35.  */
  36.  
  37.    if (argc != 3 || *argv[1] == '-')
  38.    {   fprintf (stderr, USAGE, argv[0]);
  39.        exit ((argc != 3) ? WRONG_NUMBER_OF_ARGUMENTS : OK);
  40.    }
  41.  
  42.    if ((input = fopen (argv[1], "r")) == NULL)
  43.    {   fprintf (stderr, "Unable to open input file %s\n", argv[1]);
  44.        perror (argv[0]);
  45.        exit (BAD_INPUT_FILE);
  46.    }
  47.  
  48.    if ((output = fopen (argv[2], "o")) == NULL)
  49.    {   fprintf (stderr, "Unable to open output file %s\n", argv[2]);
  50.        perror (argv[0]);
  51.        exit (BAD_OUTPUT_FILE);
  52.    }
  53.  
  54. /* We managed to open the files, now simply read an ascii file and
  55.  * write a binary one, one character at a time.
  56.  */
  57.  
  58.    while(1)
  59.    {   register c;
  60.  
  61.        if ((c = fgetc (input)) == EOF)
  62.        {   /* Real EOF or error ? */
  63.        if (feof (input)) break;
  64.     
  65.            fprintf (stderr, "Error in reading input file\n");
  66.            perror (argv[0]);
  67.            exit (BAD_INPUT_FILE);
  68.        }
  69.  
  70.        if ((fputc ((char) c, output)) == EOF)
  71.        {   fprintf (stderr, "Error in writing output file\n");
  72.            perror (argv[0]);
  73.            exit (BAD_OUTPUT_FILE);
  74.        }
  75.    } 
  76.  
  77.    (void) fclose (input);
  78.    (void) fclose (output);
  79.    exit (OK);
  80. }
  81.